from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_rag.entity.do.rag_do import Rag
from module_rag.entity.vo.rag_vo import RagModel, RagPageQueryModel
from utils.page_util import PageUtil
from module_admin.entity.do.user_do import SysUser, SysUserPost, SysUserRole

class RagDao:
    """
    知识库信息模块数据库操作层
    """

    @classmethod
    async def get_rag_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据知识库id获取知识库信息详细信息

        :param db: orm对象
        :param id: 知识库id
        :return: 知识库信息信息对象
        """
        rag_info = (
            (
                await db.execute(
                    select(Rag)
                    .where(
                        Rag.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return rag_info

    @classmethod
    async def get_rag_detail_by_info(cls, db: AsyncSession, rag: RagModel):
        """
        根据知识库信息参数获取知识库信息信息

        :param db: orm对象
        :param rag: 知识库信息参数对象
        :return: 知识库信息信息对象
        """
        rag_info = (
            (
                await db.execute(
                    select(Rag).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return rag_info

    @classmethod
    async def get_rag_list(cls, db: AsyncSession, query_object: RagPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取知识库信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 知识库信息列表信息对象
        """
        query = (
            select(Rag, SysUser)
            .where(
                Rag.rag_title.like(f'%{query_object.rag_title}%') if query_object.rag_title else True,
                # Rag.user_id == query_object.user_id if query_object.user_id else True,
                Rag.status == query_object.status if query_object.status else True,
            )
            .join(SysUser, (SysUser.user_id== Rag.user_id))
            .order_by(Rag.id)
            .distinct()
        )
        rag_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return rag_list

    @classmethod
    async def add_rag_dao(cls, db: AsyncSession, rag: RagModel):
        """
        新增知识库信息数据库操作

        :param db: orm对象
        :param rag: 知识库信息对象
        :return:
        """
        db_rag = Rag(**rag.model_dump(exclude={}))
        db.add(db_rag)
        await db.flush()

        return db_rag

    @classmethod
    async def edit_rag_dao(cls, db: AsyncSession, rag: dict):
        """
        编辑知识库信息数据库操作

        :param db: orm对象
        :param rag: 需要更新的知识库信息字典
        :return:
        """
        await db.execute(update(Rag), [rag])

    @classmethod
    async def delete_rag_dao(cls, db: AsyncSession, rag: RagModel):
        """
        删除知识库信息数据库操作

        :param db: orm对象
        :param rag: 知识库信息对象
        :return:
        """
        await db.execute(delete(Rag).where(Rag.id.in_([rag.id])))

